import json
import os
from pytube import YouTube

# Log for videos not downloaded.
log_file_path = './download_fail_test.txt'
open(log_file_path, 'w').close()

# Log for last downloaded file
log_download_path = './last_download_test.txt'

if not os.path.exists(log_download_path):
    start = -1
    open(log_download_path, 'w').close()
else:
    f = open(log_download_path, 'r')
    start = int(f.readlines()[0]) + 1
    f.close()

test_info = json.load(open('./data/test_videodatainfo.json'))


def downloadYouTube(video_id, video_url, path):

    try:
        yt = YouTube(video_url)
        yt.streams.first().download()
        os.rename(yt.streams.first().default_filename, path + video_id + '.mp4')
    except:
        video_log = open(log_file_path, 'a')
        video_log.write(video_id + '\t' + video_url + '\n')
        video_log.close()


total_videos = len(test_info['videos'])

print('total videos', total_videos)
print('downloading videos...')

# Download videos
for i, item in enumerate(test_info['videos']):
    video_id = item['video_id']
    video_url = item['url']

    if i >= start:
        downloadYouTube(video_id, video_url, './videos/test/')
        f = open(log_download_path, 'w')
        f.write(str(i))
        f.close()

    print('Downloaded video', i + 1, 'left', total_videos - i - 1)
